<template>
      <div class="company-form">
            <ul class="ul-list whitebg">
                <li>
                    <a href="javascript:void(0);" class="content">
                        <span class="ul-list-tit pull-left">公司代码</span>
                        <div class="con input-box">
                            <input  maxlength="6" v-model:value="inputCode" placeholder="请输入公司代码" id="company_code"lazy>
                        </div>
                    </a>
                </li>
                <li v-if="company.companyName">
                    <a href="javascript:void(0);" class="content">
                        <span class="ul-list-tit pull-left">公司全称</span>
                        <div class="con input-box">
                            <span>{{company.companyName}}</span>
                        </div>
                    </a>
                </li>
            </ul>
            <p class="blind-p content" @click="bindCompanyCode"><a href="javascript:void(0);" class="js_logout blind-btn">绑定</a></p>
       </div>
</template>
<script>
    export default{
        data(){
            return {
            	company:{
					companyName:"",
					companyCode:""
				},
				inputCode:""
            }
        },
        mounted: function() {
        },
        watch:{
        	'inputCode':function(val,oldVal){
        		var _self = this;
        		_self.loadCompanyInfo();
        		
        	}
        },
        methods:{
          bindCompanyCode:function(){
	          	var _self = this;
	          	if(!_self.inputCode){
	          		layer.msg("请输入公司的代码");
	          		return false;
	          	}
                var body = {};
                body.params = {
                        companyCode:_self.inputCode
                    }
				var url = this.utilHelper.apiUrl+'/api/bindCompanyCode';
				_self.$http.post(url,body).then((response)=>{
					var data = response.body.resData
					if(data.result==1){
						_self.getPartnerInfo();//更新用户的缓存信息
						layer.msg("绑定成功!");
						_self.$router.replace({path:'My'});
					}else{
						layer.msg(data.resultMsg);
					}
				})
         },
         loadCompanyInfo:function(){
         	var _self =this;
         	_self.company.companyName ="";
         	_self.company.companyCode ="";
         	if(_self.inputCode.length==6){
                var body = {};
         		body.params = {
      			     companyCode:_self.inputCode
				}
				var url = this.utilHelper.apiUrl+'/api/getAgentOrgByCompanyCode';
				_self.$http.post(url,body).then((response)=>{
    				var data = response.body.resData
    				if(data.result==1){
    					_self.company = data.company;
    					if(!data.company.companyName){
    						layer.msg("未找到匹配公司");
    					}
    				}else{
    					layer.msg(data.resultMsg);
    				}
				})
         	}
         },
        //实时获取用户信息
        getPartnerInfo(){
            var _self = this;
            var body = {};
            body.params = {}
            var url = this.utilHelper.apiUrl+'/api/getUserById';
              _self.$http.post(url, body).then((response) => {
                  var data = response.body.resData;
                  var result = data.result;
                  if (result == "1") {
                    window.localStorage.setItem("3_userId", data.id);
                    window.localStorage.setItem("3_userName", data.accountName);
                    window.localStorage.setItem("3_phone", data.phone);
                    window.localStorage.setItem("3_isLogin", true);
                    window.localStorage.setItem("3_userRole", data.userRoleId);
                    window.localStorage.setItem("3_accountType",data.accountType);
                  }
              });
        }
      }
    }
    
</script>
<style scoped>
    @import url("../../assets/css/global.css");
    @import url("../../assets/css/style.css");
    	input, textarea, img {
		    vertical-align: middle;
			    font-family: Arial, "Microsoft Yahei";
			}
			input, textarea {
			    outline: none;
			}
			input, textarea, button {
			    -webkit-appearance: none;
			}
			input, textarea, button {
	    -webkit-appearance: none;
			}
</style>
 No newline at end of file
